package GitObjController

import (
	"arabApi/app/api/controllers"
	"arabApi/app/api/controllers/CFun"
	"arabApi/app/api/requests"
	"arabApi/app/api/requests/CRequest"
	"arabApi/app/api/requests/ObjGitRequest"
	"arabApi/app/api/services/ObjGitService"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type GitObj struct {
	controllers.BaseAPIController
}

func (r GitObj) List(c *gin.Context) {

	objreq := CRequest.Cpage{}

	if err := c.ShouldBindQuery(&objreq); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("解析错误"+err.Error()))
		return
	}

	data := gin.H{}
	data["list"], data["count"] = ObjGitService.List(objreq)

	CFun.Ajax_return_success(c, CFun.WithData(data))

}

func (r GitObj) MsgById(c *gin.Context) {
	id := CRequest.Oid{}

	if err := c.ShouldBindQuery(&id); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("未找到对应的ID"+err.Error()))
		return
	}
	if ok := requests.Validate(c, &id, CRequest.CheckID); !ok {
		return
	}

	msg := ObjGitService.MsgByID(id.Id)
	CFun.Ajax_return_success(c, CFun.WithData(msg))
}

func (r GitObj) Edit(c *gin.Context) {

	s := ObjGitRequest.ObjGitRequest{}
	if err := c.ShouldBindBodyWith(&s, binding.JSON); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("未找到对应的ID"+err.Error()))
		return
	}
	if ok := requests.Validate(c, &s, ObjGitRequest.CheckGitObj); !ok {
		return
	}

	if err := ObjGitService.Edit(s); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("编辑失败"+err.Error()))
	} else {
		CFun.Ajax_return_success(c)
	}

}

func (r GitObj) Add(c *gin.Context) {

	s := ObjGitRequest.ObjGitRequest{}
	if err := c.ShouldBindBodyWith(&s, binding.JSON); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("未找到对应的ID"+err.Error()))
		return
	}
	if ok := requests.Validate(c, &s, ObjGitRequest.CheckGitObj); !ok {
		return
	}

	if id, err := ObjGitService.Add(s); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("添加失败"+err.Error()))
	} else {
		CFun.Ajax_return_success(c, CFun.WithData(id), CFun.WithMsg("添加成功"))
	}
}
